Developer Documentation

QuickTime 4 API Documentation

QuickTime for Java

| Previous | Chapter Top | Next |

The QTSimpleApplet Code

The sample code QTSimpleApplet (available in the SDK that comes with the book QuickTime for Java) is a QuickTime for Java applet you can create that presents any of the media file formats that QuickTime supports. QuickTime includes support for a vast array of common file formats: QuickTime movies (including QuickTime VR), pictures, sounds, MIDI, and QuickDraw 3D.

The applet tag for this applet is

<applet code="QTSimpleApplet.class" width=200 height=100>
    <param name="file" value="media/crossfad.gif">
</applet>

The QTSimpleApplet code takes any of the media file types supported by QuickTime as a parameter in the HTML applet tag and creates the appropriate object for that media type:

param name=file value="MyMediaFile.xxx"

The QTCanvas , QTDrawable , and QTFactory classes, which are part of the QTSimpleApplet code, are discussed in greater detail in the next section.

As with all Java applets, we begin in the QTSimpleApplet code by declaring a list of Java packages and QuickTime for Java packages that contain the required classes you need to import:

import java.applet.Applet;
import java.awt.*;
import quicktime.QTSession;
import quicktime.io.QTFile;

import quicktime.app.QTFactory;
import quicktime.app.display.QTCanvas;
import quicktime.app.display.Drawable;
import quicktime.QTException;

To get the resources for the simple applet and set up the environment, including the creation of the QTCanvas object, you use the init() method, as shown in the snippet below. QTCanvas is the object responsible for handling the integration from the java.awt side between Java and QuickTime. The QTCanvas also has parameters that let you control the resizing of the client that it presents. In this case, we tell the canvas to center the client within the space given by the applet's layout manager. This ensures that the client is only as big as its initial size (or smaller if you make the canvas smaller).

The QTSession.open() call performs a gestalt check to make sure that QuickTime is present and is initialized. Note that this is a required call before any QuickTime for Java classes can be used:

public void init () {
    try {
        QTSession.open()

To set up a QTCanvas object that displays its content at its original size or smaller and is centered in the space given to the QTCanvas when the applet is laid out, we do the following:

        setLayout (new BorderLayout());
        myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F,
                0.5F);
        add (myQTCanvas, "Center");     
    
        QTFile file = new QTFile (getCodeBase().getFile() +
                                    getParameter("file"));
        myQTContent = QTFactory.makeDrawable (file);
    } catch (Exception e) {
        e.printStackTrace();
        ...
    }
}

The QTFactory.makeDrawable() method is used to create an appropriate QuickTime object for the media that is specified in the <PARAM> tag.

If a QTException is thrown in the init() method, an appropriate action should be taken by the applet, depending on the error reported.

In the start() method shown in the next code snippet, you set the client of the QTCanvas . This QTCanvas.client is the QuickTime object (i.e., an object that implements the QTDrawable interface) that draws to the area of the screen that the QTCanvas occupies. This is the QuickTime side of the integration between Java and QuickTime:

public void start () {
    try { myQTCanvas.setClient (myQTContent, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

You use the stop() method to remove the client from the QTCanvas . It will be reset in the start() method if the applet is restarted. destroy() is used to close the QTSession . This protocol enables the applet to be reloaded, suspended, and resumed--for example, if the user is leaving and returning to the page with the applet. The init() / destroy() , and start() / stop() methods are reciprocal in their activities.

public void stop () {
    myQTCanvas.removeClient();
}

public void destroy () {
    QTSession.close();
}

You need to call QTSession.close() if you have previously called QTSession.open() in order to shut down QuickTime properly.

The init() method may throw exceptions because the required file was not found or the applet does not have permission from Java's security manager to read that file. Alternatively, the required version of QuickTime may not be installed. The applet should deal with these issues appropriately.


© 1999 Apple Computer, Inc.

| Previous | Chapter Top | Next |